package com.hjm.app.fragment.order;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.material.divider.MaterialDivider;

import com.hjm.app.R;
import com.hjm.app.activity.OrderActivity;
import com.hjm.java.entity.Address;
import com.hjm.ui.order.OrderItem;
import com.hjm.ui.order.entity.Order;

import java.util.ArrayList;
import java.util.List;

public class FragmentOrderDetail extends Fragment {
    private TextView rxAddressTextView, txAddressTextView;
    private Button cancelButton, greenButton;
    private MaterialDivider divider01,divider02;
    private Address rxAddress, txAddress;
    private String rxAddressStr, txAddressStr;
    private List<Order> orderList;
    private OrderItem orderItem;
    private String type;
    private OrderCallBackInterface orderCallBackInterface;

    public void setOrderCallBackInterface(OrderCallBackInterface orderCallBackInterface) {
        this.orderCallBackInterface = orderCallBackInterface;
        setView();
    }

    public FragmentOrderDetail() {
    }

    public static FragmentOrderDetail newInstance(String type, Order order) {
        FragmentOrderDetail fragmentOrderDetail = new FragmentOrderDetail();
        Bundle args = new Bundle();
        args.putString("type", type);
        args.putParcelable("order", order);
        fragmentOrderDetail.setArguments(args);
        return fragmentOrderDetail;
    }

    public static FragmentOrderDetail newInstance(String type, List<Order> orderList) {
        FragmentOrderDetail fragmentOrderDetail = new FragmentOrderDetail();
        Bundle args = new Bundle();
        args.putString("type", type);
        args.putParcelableArrayList("orderList", (ArrayList<? extends Parcelable>) orderList);
        fragmentOrderDetail.setArguments(args);
        return fragmentOrderDetail;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            // 两种模式一种是只传一个订单，一种是传一个订单列表
            Order order = getArguments().getParcelable("order");
            List<Order> orderList = getArguments().getParcelableArrayList("orderList");
            if (order != null) {
                this.orderList = new ArrayList<>();
                this.orderList.add(order);
            } else if (orderList != null) {
                this.orderList = orderList;
            }
            type = getArguments().getString("type");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_detail, container, false);
        rxAddressTextView = view.findViewById(R.id.text_view_order_detail_rx_address);
        txAddressTextView = view.findViewById(R.id.text_view_order_detail_tx_address);
        divider01 = view.findViewById(R.id.divider_order_detail_address_01);
        divider02 = view.findViewById(R.id.divider_order_detail_address_02);
        if (rxAddress != null)
            rxAddressTextView.setText(rxAddressStr);
        if (txAddress != null)
            txAddressTextView.setText(txAddressStr);

        orderItem = view.findViewById(R.id.order_item_order_detail);
        if (orderList != null)
            orderItem.setOrderList(orderList);
        orderItem.setOnItemClickListener((v)->{
            orderCallBackInterface.onButtonClickListener("点击商品", this.orderList);
        });
        cancelButton = view.findViewById(R.id.button_order_detail_cancel);
        greenButton = view.findViewById(R.id.button_order_detail_green);
        setView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void setRxAddress(Address rxAddress) {
        this.rxAddress = rxAddress;
        rxAddressStr = "收件人姓名：" + rxAddress.getName() +
                "\n收件人电话：" + rxAddress.getPhone() +
                "\n收件人地址：" + rxAddress.getAddress();
        if (rxAddressTextView != null)
            rxAddressTextView.setText(rxAddressStr);
    }

    public void setTxAddress(Address txAddress) {
        this.txAddress = txAddress;
        txAddressStr = "发件人姓名：" + txAddress.getName() +
                "\n发件人电话：" + txAddress.getPhone() +
                "\n发件人地址：" + txAddress.getAddress();
        if (txAddressTextView != null)
            txAddressTextView.setText(txAddressStr);
    }

    public void setOrder(Order order) {
        List<Order> orderList = new ArrayList<>();
        orderList.add(order);
        this.orderList = orderList;
        if (orderItem != null)
            orderItem.setOrderList(orderList);
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
        if (orderItem != null)
            orderItem.setOrderList(orderList);
    }

    /**
     * 根据订单的不同状态设置不同的组件内容，不同的点击事件
     */
    void setView() {
        if (greenButton != null && cancelButton != null && type != null && orderCallBackInterface != null) {
            switch (type) {
                case OrderActivity.TYPE_ORDER_NO_PAY:
                    greenButton.setText("支付");
                    cancelButton.setText("取消订单");
                    txAddressTextView.setVisibility(View.GONE);
                    divider01.setVisibility(View.GONE);
                    rxAddressTextView.setVisibility(View.VISIBLE);
                    divider02.setVisibility(View.VISIBLE);
                    greenButton.setOnClickListener((v)->{
                       orderCallBackInterface.onButtonClickListener("支付",this.orderList);
                    });
                    cancelButton.setOnClickListener((v)->{
                        orderCallBackInterface.onButtonClickListener("未支付-取消订单",this.orderList);
                    });
                    break;
                case OrderActivity.TYPE_ORDER_NO_TX:
                    greenButton.setText("提醒发货");
                    cancelButton.setText("取消订单");
                    txAddressTextView.setVisibility(View.GONE);
                    divider01.setVisibility(View.GONE);
                    rxAddressTextView.setVisibility(View.VISIBLE);
                    divider02.setVisibility(View.VISIBLE);
                    greenButton.setOnClickListener((v)->{
                        orderCallBackInterface.onButtonClickListener("提醒发货",this.orderList);
                    });
                    cancelButton.setOnClickListener((v)->{
                        orderCallBackInterface.onButtonClickListener("待发货-取消订单",this.orderList);
                    });
                    break;
                case OrderActivity.TYPE_ORDER_NO_RX:
                    greenButton.setText("确认收货");
                    cancelButton.setVisibility(View.GONE);
                    rxAddressTextView.setVisibility(View.VISIBLE);
                    divider01.setVisibility(View.VISIBLE);
                    txAddressTextView.setVisibility(View.VISIBLE);
                    divider02.setVisibility(View.VISIBLE);
                    greenButton.setOnClickListener((v)->{
                        orderCallBackInterface.onButtonClickListener("确认收货",this.orderList);
                    });
                    break;
                case OrderActivity.TYPE_ORDER_DONE:
                    greenButton.setText("商品评价");
                    cancelButton.setText("店铺评价");
//                    rxAddressTextView.setVisibility(View.GONE);
//                    divider01.setVisibility(View.GONE);
//                    txAddressTextView.setVisibility(View.GONE);
//                    divider02.setVisibility(View.GONE);
                    rxAddressTextView.setVisibility(View.VISIBLE);
                    divider01.setVisibility(View.VISIBLE);
                    txAddressTextView.setVisibility(View.VISIBLE);
                    divider02.setVisibility(View.VISIBLE);
                    greenButton.setOnClickListener((v)->{
                        orderCallBackInterface.onButtonClickListener("商品评价",this.orderList);
                    });
                    cancelButton.setOnClickListener((v)->{
                        orderCallBackInterface.onButtonClickListener("店铺评价",this.orderList);
                    });
                    break;
            }
        }
    }
}